<script setup>
import {productAddService,productCategoryListService,productDeleteService,productEditService,productListWithConditionService} from '@/api/catalog'
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
//导入token
import { userTokenStore } from '@/store/token'
const tokenStore = userTokenStore();
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { ElMessage,ElMessageBox } from 'element-plus'

//产品所属分类数据模型
const categorys = ref(["AV-CB-01", "AV-SB-02", "FI-FW-01", "FI-SW-01", "FI-SW-02", "FI-SW-03"])
//产品数据模型
const products = ref([
    {
        "productId": "AV-CB-01",
        "categoryId": "BIRDS",
        "name": "Amazon Parrot3",
        "description": "Great companion for up to 75 years",
        "img":"123456"
    },
    {
        "productId": "AV-CB-01",
        "categoryId": "DOGS",
        "name": "Amazon Parrot1",
        "description": "Great companion for up to 75 years",
        "img":"123456"
    },
    {
        "productId": "AV-CB-01",
        "categoryId": "MANS",
        "name": "Amazon Parrot2",
        "description": "Great companion for up to 75 years",
        "img":"123456"
    }
])
//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(10)//每页条数
//用户搜索时选中的产品id
const categoryIdForSearch = ref('')

//使用标题共用编辑框
const title = ref('');
//控制抽屉是否显示
const visibleDrawer = ref(false)

//添加表单数据模型
const productModel = ref({
    productId: '',
    categoryId: '',
    name:'',
    description:'',
    img:''
})

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size;
    getProductWithCondition();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num;
    getProductWithCondition();
}

//获取product产品所属分类列表
const getProductCategory = async () => {
    let result = await productCategoryListService();
    if (result) {
        categorys.value = result.data;

    }
}
getProductCategory();
//获取product产品详情
const getProductWithCondition = async () => {
    //处理数据
    let params = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        categoryIdForSearch: categoryIdForSearch.value ? categoryIdForSearch.value : null
    }
    let result = await productListWithConditionService(params);
    if (result) {
        total.value = result.data.total;
        products.value = result.data.list;
    }
}
getProductWithCondition();


//清空数据回显
const clearData = () => {
    productModel.value.productId = '';
    productModel.value.categoryId = '';
    productModel.value.name = '';
    productModel.value.description = '';
    productModel.value.img = '';

}
//展示添加drawer
const showAddDrawer = () => {
    clearData();
    title.value = '添加产品';
    visibleDrawer.value = true;
}
//上传回显函数
const uploadSuccess = (result) => {
    productModel.value.img = result.data;
    // console.log(articleModel);
    // console.log(result.data);
}
//展示编辑drawer
const showEditDrawer = (row) => {
    title.value = '编辑产品';
    visibleDrawer.value = true;    
    //显示数据
    productModel.value.productId = row.productId;
    productModel.value.categoryId = row.categoryId;
    productModel.value.name = row.name;
    productModel.value.description = row.description;
    productModel.value.img = row.img;
}

//添加product
const add = async () => {
    let result = await productAddService(productModel.value);
    ElMessage(result.message ? result.message : '添加成功');
    visibleDrawer.value = false;
    //保持条件查找
    getProductWithCondition();
}
//编辑product
const edit = async () => {
    let result = await productEditService(productModel.value);
    ElMessage(result.message ? result.message : '编辑成功');
    visibleDrawer.value = false;
    //保持条件查找
    getProductWithCondition();
}
//发布产品，可以是添加产品或者编辑产品
const publishProduct = () => {
    title.value === '添加产品' ? add() : edit();
}

//删除产品
const deletefor = async (productid) => {
    let result = await productDeleteService(productid);
    ElMessage(result.message ? result.message : '删除成功');
    getProductWithCondition();
}
//删除产品弹窗
const deleteBox = (productid) => {
    ElMessageBox.confirm(
        '您确定要删除这个产品吗',
        '注意',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            deletefor(productid);
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '删除取消',
            })
        })
}



//有条件搜索
const search = () => {
    getProductWithCondition();
    ElMessage('获取成功');
}
//无条件搜索,用于重置
const searchWithNoCondition = () => {
    categoryIdForSearch.value = null;
    getProductWithCondition();
    ElMessage('获取成功');
}

</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>Product产品管理</span>
                <div class="extra">
                    <el-button type="primary" @click="showAddDrawer()">添加产品</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="所属分类：">
                <el-select placeholder="请选择" v-model="categoryIdForSearch" style="width: 100px;">
                    <!-- products我们会得到这些数据，这些数据所拥有的 -->
                    <el-option v-for="c in categorys" :key="c" :label="c" :value="c">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="search()">搜索</el-button>
                <el-button @click="searchWithNoCondition()">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- Product列表 -->
        <el-table :data="products" style="width: 100%">
            <el-table-column label="产品Id" prop="productId"></el-table-column>
            <el-table-column label="所属分类Id" prop="categoryId"></el-table-column>
            <el-table-column label="名字" prop="name"> </el-table-column>
            <el-table-column label="描述" prop="description"></el-table-column>
            <el-table-column label="操作" width="160">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showEditDrawer(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteBox(row.productId)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
            <!-- 添加/编辑产品表单 -->
            <el-form :model="productModel" label-width="100px">
                <el-form-item label="产品id">
                    <el-input v-model="productModel.productId" placeholder="请输入" :disabled="title === '编辑产品'" ></el-input>
                </el-form-item>
                <el-form-item label="所属分类id">
                    <el-select placeholder="请选择" v-model="productModel.categoryId">
                        <el-option v-for="c in categorys" :key="c" :label="c" :value="c">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="名字">
                    <el-input v-model="productModel.name" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="productModel.description" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" width= "500px" @click="publishProduct()">完成</el-button>
                </el-form-item>
                <!-- 完成按钮增大宽度todo -->
                 <!--todo 图标*3 -->
            </el-form>
        </el-drawer>
    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}

/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>